﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Contract.Data;
using System.Collections.ObjectModel;
using DotNetChatServer.Interfaces;

namespace DotNetChatServer
{
    public class SimpleMessageStorage : IMessageStorage
    {
        private object myListLock = new object();
        List<ChatMessage> myMessages = new List<ChatMessage>();
        private const int MAX_MESSAGES = 50;

        #region IMessageStorage Members

        public void DeleteMessage(Guid messsageId)
        {
            lock (myListLock)
            {
                ChatMessage deleteMsg = (from msg in myMessages where msg.MessageId == messsageId select msg).FirstOrDefault<ChatMessage>();
                if (deleteMsg != null)
                    myMessages.Remove(deleteMsg);
            }
        }

        public void AddMessage(ChatMessage msg)
        {
            lock (myListLock)
            {

                myMessages.Add(msg);
                msg.TimeStamp = DateTime.Now;
                MaintainMessageList();
            }
        }

        private void MaintainMessageList() {
            lock (myListLock)
            {
                if (myMessages.Count > MAX_MESSAGES)
                {
                    var earliestMsg = myMessages[myMessages.Count - MAX_MESSAGES];
                    myMessages.RemoveAll(msg => msg.TimeStamp < earliestMsg.TimeStamp);
                }
            }
        }

        public List<ChatMessage> GetMessages(DateTime fromTime, DateTime toTime)
        {
            lock (myListLock)
            {

                return (from msg in myMessages where msg.TimeStamp >= fromTime && msg.TimeStamp < toTime select msg).ToList<ChatMessage>();
            }
        }


        public List<ChatMessage> GetMessages(DateTime fromTime)
        {
            lock (myListLock)
            {

                return (from msg in myMessages where msg.TimeStamp >= fromTime select msg).ToList<ChatMessage>();
            }
        }

        public List<ChatMessage> GetMessages()
        {
            List<ChatMessage> ret = new List<ChatMessage>();
            lock (myListLock)
            {
                ret.AddRange(myMessages);
            }
            // must return a copy of the list.
            return ret;
        }

        public void ClearMessages(DateTime fromTime, DateTime toTime)
        {
            lock (myListLock)
            {
                myMessages.RemoveAll(msg => msg.TimeStamp >= fromTime && (toTime == DateTime.MaxValue || msg.TimeStamp < toTime));
            }
        }

        public void ClearMessages(DateTime to)
        {
            lock (myListLock)
            {
                myMessages.RemoveAll(msg => msg.TimeStamp < to);
            }
        }

        public void ClearMessages()
        {
            lock (myListLock)
            {
                myMessages.Clear();
            }
        }

        #endregion

    }
}

